<template>
  <div class="ValidCode disabled-select" style="width: 100%; height: 100%" @click="refreshCode">
    <span v-for="(item,idx) in codeList" :key="idx" :style="getStyle(item)">{{ item.code }}</span>
  </div>
</template>

<script>
export default {
  name: "validCode",
  data() {
    return {
      length: 4,
      codeList: []
    }
  },
  mounted() {
    this.createCode();
  },
  methods: {
    refreshCode() {
      this.createCode()
    },
    createCode(){
      let len = this.length,
          codeList = [],
          chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
          charsLen = chars.length;
      for (let i = 0; i < len; i++) {
        let rgb = [Math.round(Math.random() * 320), Math.round(Math.random() * 340), Math.round(Math.random() * 300)]
        codeList.push({
          code: chars.charAt(Math.floor(Math.random() * charsLen)),
          color: `rgb(${rgb.join(',')})`,
          fontSize: `${Math.random() * 20 + 20}px`,
          padding: `${Math.random() * 5}px`,
          transform: `rotate(${Math.random() * 60}deg)`
        })
      }
      this.codeList = codeList
      this.$emit('update:value', codeList.map(item => item.code).join(''))
    },
    getStyle(data) {
      return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
    }
  },
};
</script>


<style>
.ValidCode {
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.ValidCode span {
  display: inline-block;
  font-size: 18px;
}
</style>

